/*
 * FavoriteTestPanel.cpp
 *
 *  Created on: 2012-11-3
 *      Author: tutu
 */

#include "FavoriteTestPanel.h"
#include "../TContext.h"
#include <QMessageBox>

FavoriteTestPanel::FavoriteTestPanel(QWidget *parent) : QWidget(parent)
{
	_idEdit = new QLineEdit(this);
	_idEdit->setText("Input Ids");
	_idEdit->setGeometry(10, 10, 150, 30);

	_favoriteButton = new QPushButton(this);
	_favoriteButton->setText("Favorite");
	_favoriteButton->setGeometry(10, 50, 100, 30);
	connect(_favoriteButton, SIGNAL(clicked()), this, SLOT(favoriteButtonClicked()));

	_unfavoriteButton = new QPushButton(this);
	_unfavoriteButton->setText("Unfavorite");
	_unfavoriteButton->setGeometry(10, 90, 100, 30);
	connect(_unfavoriteButton, SIGNAL(clicked()), this, SLOT(unfavoriteButtonClicked()));

	_requester = TContext::instance()->requesterFactory()->createFavoriteRequester();
	connect(_requester.data(), SIGNAL(favoriteFinished(bool)), this, SLOT(favoriteFinished(bool)));
	connect(_requester.data(), SIGNAL(unfavoriteFinished(bool)), this, SLOT(unfavoriteFinished(bool)));
}

FavoriteTestPanel::~FavoriteTestPanel()
{
	// TODO Auto-generated destructor stub
}

void FavoriteTestPanel::favoriteButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		if(temp.size()==1)
		{
			_requester->favoriteDocument(temp.at(0).toInt());
		}
		else
		{
			QList<int> idList;
			for(int i=1; i<=temp.size(); i++)
			{
				idList.append(temp.at(i-1).toInt());
			}
			_requester->favoriteDocuments(idList);
		}
	}
}

void FavoriteTestPanel::unfavoriteButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		if(temp.size()==1)
		{
			_requester->unfavoriteDocument(temp.at(0).toInt());
		}
		else
		{
			QList<int> idList;
			for(int i=1; i<=temp.size(); i++)
			{
				idList.append(temp.at(i-1).toInt());
			}
			_requester->unfavoriteDocuments(idList);
		}
	}
}

void FavoriteTestPanel::favoriteFinished(bool error)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Favorite Success.");
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Favorite Failure.");
	}
}

void FavoriteTestPanel::unfavoriteFinished(bool error)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Unfavorite Success.");
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Unfavorite Failure.");
	}
}
